package com.rankeiot.platform.util;

import com.rankeiot.core.data.response.Resp;
import com.rankeiot.core.util.StringUtil;
import com.rankeiot.platform.config.PlatformConfig;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;

public final class PasswordUtil {
    //弱密码检测
    public static final Pattern INCLUDE_ALL_REGEX = Pattern
            .compile("(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\\d)(?=.*?[!@#$%^&*.=\\-\\+()]).+");
    public static final int PASSWORD_MIN_LENGTH=8;

    /**
     * 检测密码是否符合要求，并抛出对应的错误
     * @param password
     */
    public static void checkWeakPassword(String password){
       if(PlatformConfig.PasswordCheck.value().asBoolean()){
           if(password.length()<PASSWORD_MIN_LENGTH){
               Resp.fail("密码长度不足"+PASSWORD_MIN_LENGTH+"位",5000);
           }
           if(!INCLUDE_ALL_REGEX.matcher(password).matches()){
               Resp.fail("密码中必须包含大小写字母、数字以及符号!",5000);
           }
       }
    }

    /**
     * 检测密码是否为弱密码
     * @param password
     * @return
     */
    public static boolean isWeakPassword(String password){
        if(PlatformConfig.PasswordCheck.value().asBoolean()){
            if(password.length()<PASSWORD_MIN_LENGTH){
                return true;
            }
            return !INCLUDE_ALL_REGEX.matcher(password).matches();
        }
        return false;
    }


    private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String DIGITS = "0123456789";
    private static final String SPECIAL = "!@#$%^&*()-_+=";

    /**
     * 生成一个随机的安全密码
     * @param length 密码长度,如果小于8，则默认为8
     * @return
     */
    public static String generatePassword(int length) {
        // 确保长度至少为8
        if (length < 8) {
            length=8;
        }
        String allChars = UPPER + LOWER + DIGITS + SPECIAL;
        StringBuilder password = new StringBuilder();
        Random random=new Random();
        // 确保至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符
        password.append(UPPER.charAt(random.nextInt(UPPER.length())));
        password.append(LOWER.charAt(random.nextInt(LOWER.length())));
        password.append(DIGITS.charAt(random.nextInt(DIGITS.length())));
        password.append(SPECIAL.charAt(random.nextInt(SPECIAL.length())));

        // 剩余随机生成的字符
        for (int i = 4; i < length; i++) {
            int index = new Random().nextInt(allChars.length());
            password.append(allChars.charAt(index));
        }

        // 将密码随机排序
        List<Character> passwordList = new ArrayList<>();
        for (char c : password.toString().toCharArray()) {
            passwordList.add(c);
        }
        Collections.shuffle(passwordList);

        StringBuilder shuffledPassword = new StringBuilder();
        for (char c : passwordList) {
            shuffledPassword.append(c);
        }

        return shuffledPassword.toString();
    }
    public static String encodePassword(String password,String username){
       return StringUtil.md5Hex(username + "$" + password);
    }
}
